package com.ruoyi.project.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.GetLoginUser;
import com.ruoyi.project.service.IProjectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.domain.ProjectRisk;
import com.ruoyi.project.service.IProjectRiskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目风险Controller
 * 
 * @author renyuxin
 * @date 2024-06-05
 */
@RestController
@RequestMapping("/project/projectRisk")
public class ProjectRiskController extends BaseController
{
    @Autowired
    private IProjectRiskService projectRiskService;

    @Autowired
    private IProjectService projectService;

    /**
     * 查询项目风险列表
     */
    @PreAuthorize("@ss.hasPermi('project:projectRisk:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProjectRisk projectRisk)
    {
        // 找到登录用户所能看见的项目ids
        projectRisk.setProjectIds(projectService.selectProjectIds(GetLoginUser.id()));

        startPage();
        List<ProjectRisk> list = projectRiskService.selectProjectRiskList(projectRisk);
        return getDataTable(list);
    }

    /**
     * 导出项目风险列表
     */
    @PreAuthorize("@ss.hasPermi('project:projectRisk:export')")
    @Log(title = "项目风险", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectRisk projectRisk)
    {
        // 找到登录用户所能看见的项目ids
        projectRisk.setProjectIds(projectService.selectProjectIds(GetLoginUser.id()));

        List<ProjectRisk> list = projectRiskService.selectProjectRiskList(projectRisk);
        ExcelUtil<ProjectRisk> util = new ExcelUtil<ProjectRisk>(ProjectRisk.class);
        util.exportExcel(response, list, "项目风险数据");
    }

    /**
     * 获取项目风险详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:projectRisk:query')")
    @GetMapping(value = "/{riskId}")
    public AjaxResult getInfo(@PathVariable("riskId") Long riskId)
    {
        return success(projectRiskService.selectProjectRiskByRiskId(riskId));
    }

    /**
     * 新增项目风险
     */
    @PreAuthorize("@ss.hasPermi('project:projectRisk:add')")
    @Log(title = "项目风险", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ProjectRisk projectRisk)
    {
        return toAjax(projectRiskService.insertProjectRisk(projectRisk));
    }

    /**
     * 修改项目风险
     */
    @PreAuthorize("@ss.hasPermi('project:projectRisk:edit')")
    @Log(title = "项目风险", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ProjectRisk projectRisk)
    {
        return toAjax(projectRiskService.updateProjectRisk(projectRisk));
    }

    /**
     * 删除项目风险
     */
    @PreAuthorize("@ss.hasPermi('project:projectRisk:remove')")
    @Log(title = "项目风险", businessType = BusinessType.DELETE)
	@DeleteMapping("/{riskIds}")
    public AjaxResult remove(@PathVariable Long[] riskIds)
    {
        return toAjax(projectRiskService.deleteProjectRiskByRiskIds(riskIds));
    }
}
